﻿Public Class frmSystemLog
    Public sqlDK As String

    Public Sub New(Optional ByVal logContent As String = "")

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        txtLogContent.Text = logContent
    End Sub



    Private Sub frmSystemLog_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        InitSearchControls()
        BindingDataGridView(PopulateDK())
        FormatDataGridView()
    End Sub

    Public Sub BindingDataGridView(Optional ByVal strDK As String = "")
        Dim ds As DataTable = CLogs.GetLogList(strDK).Tables(0)
        Dim source As New BindingSource
        source.DataSource = ds
        DataGridViewLog.DataSource = ds
        BindingNavigator1.BindingSource = source

        For count As Integer = 0 To DataGridViewLog.RowCount - 1
            DataGridViewLog.Rows(count).HeaderCell.Value = (count + 1).ToString
        Next
    End Sub

    Public Sub FormatDataGridView()
        DataGridViewLog.AllowUserToAddRows = False
        DataGridViewLog.AllowUserToDeleteRows = False
        DataGridViewLog.ReadOnly = True
        DataGridViewLog.SelectionMode = DataGridViewSelectionMode.CellSelect
        DataGridViewLog.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells

        

        With DataGridViewLog
            .RowHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
            .Columns.Item("id_log").Visible = False
            .Columns.Item("id_action").Visible = False
            .Columns.Item("id_user").Visible = False

            .Columns.Item("ActionName").HeaderText = "Hành động"
            .Columns.Item("LogContent").HeaderText = "Nội dung"
            .Columns.Item("LogDate").HeaderText = "Ngày thực hiện"
            .Columns.Item("LoginName").HeaderText = "Người thực hiện"
            .Columns.Item("fullname").HeaderText = "Tên đầy đủ"
            .Columns.Item("ip").HeaderText = "Địa chỉ ip"
            .Columns.Item("hostname").HeaderText = "Tên máy"
        End With
    End Sub

    Private Sub btnLoc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoc.Click
        Dim sqlDK As String = PopulateDK()
        BindingDataGridView(sqlDK)
    End Sub

    Private Sub InitSearchControls()
        Dim query = CActions.GetActionList()
        cmbAction.ValueMember = "ID_Action"
        cmbAction.DisplayMember = "ActionName"
        cmbAction.DataSource = query
        cmbAction.SelectedIndex = -1

        chkAction.Checked = False
        chkAction_CheckedChanged(Nothing, Nothing)
        chkDate.Checked = False
        chkDate_CheckedChanged(Nothing, Nothing)
    End Sub

    Public Function PopulateDK() As String
        Dim sqlCon As String = ""
        If cmbAction.SelectedValue IsNot Nothing And cmbAction.Enabled Then
            sqlCon = String.Format(" Id_Action = {0}", cmbAction.SelectedValue)
        End If

        If txtUser.Text <> "" Then
            If Len(sqlCon) > 0 Then
                sqlCon += " and "
            End If
            sqlCon += String.Format("Id_User in (select User_ID from tblUser where loginname like N'%{0}%')", txtUser.Text)
        End If

        If txtLogContent.Text <> "" Then
            If Len(sqlCon) > 0 Then
                sqlCon += " and "
            End If
            sqlCon += String.Format("LogContent like N'%{0}%'", txtLogContent.Text)
        End If

        If dtFromDate.Enabled Then
            If Len(sqlCon) > 0 Then
                sqlCon += " and "
            End If
            sqlCon += String.Format("LogDate >= '{0}' and LogDate <= '{1}'", dtFromDate.Value, dtToDate.Value)
        End If
        Return sqlCon
    End Function

    Private Sub chkDate_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkDate.CheckedChanged
        dtFromDate.Enabled = chkDate.Checked
        dtToDate.Enabled = chkDate.Checked
    End Sub

    Private Sub chkAction_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAction.CheckedChanged
        cmbAction.Enabled = chkAction.Checked
    End Sub

    Private Sub btnHuy_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        chkAction.Checked = False
        chkDate.Checked = False

        cmbAction.SelectedIndex = -1
        txtUser.Text = ""
        txtLogContent.Text = ""
        BindingDataGridView()
    End Sub
End Class